<?php  
/*知识点模型*/
class Knowledge_point_model extends CI_Model
{

	var $table='knowledge_point';

	function __construct()
	{
		parent::__construct();
		
	}

	/**
	 * 获得所有的条目
	 * @return [type] [description]
	 */
	function get_all()
	{
		$res=$this->db->get($this->table)->result_array();
		return $res;
	}



	/**
	 * 通过id返回条目
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function get_by_id($id)
	{
		return $this->db->get_where($this->table, array('id' => $id ))->row_array();
	}


	/**
	 * 通过knowledge_point_id返回相关问题条目
	 * @param  [type] $knowledge_point_id [description]
	 * @return [type]                     [description]
	 */
	function get_question_by_knowledge_point_id($knowledge_point_id)
	{
		return $this->db->get_where('question_in_knowledge_point', array('knowledge_point_id' => $knowledge_point_id ))->result_array();
	}

	/**
	 * 添加一个条目
	 * @param [type] $item [description]
	 */
	function add($item)
	{
		return	$this->db->insert($this->table,$item);
	}

	/**
	 * 删除一个条目
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function delete($id)
	{
		return $this->db->delete($this->table, array('id' => $id ));

	}

	/**
	 * 更新一个条目
	 * @param  [type] $id   [description]
	 * @param  [type] $item [description]
	 * @return [type]       [description]
	 */
	function update($id,$item)
	{
		$this->db->where('id', $id);
		return $this->db->update($this->table,$item);
	}
	
	/**
	 * 返回数据表总条目个数
	 * @return [type] [description]
	 */
	function count_all()
	{
		return $this->db->count_all($this->table);
	}


	/**
	 * 获取所有的知识点,包括相关考点
	 * @return [type] [description]
	 */
	function get_all_with_test_point()
	{
		$res=$this->db->get('knowledge_point')->result_array();
		for ($i=0; $i < count($res); $i++) { 
			if($res[$i]['test_point_id'])
			{
				$query=$this->db->get_where('test_point', array('id' => $res[$i]['test_point_id']) );
				$row=$query->row_array();
				$res[$i]['test_point_content']=$row['content'];
			}	
			else
			{
				$res[$i]['test_point_content']=NULL;
			}
		}
		return $res;
	}

	function get_all_knowledge_points_by_offset($limit,$offest)
	{
		$sql='select k.id, substr(k.content,1,40) as content,substr(tp.content,1,25) as test_point_content from knowledge_point as k , test_point tp where k.test_point_id=tp.id LIMIT ? OFFSET ?';
		return $this->db->query($sql,array($limit,$offest))->result_array();
	}


	/**
	 * 添加一个知识点，通过考点一起添加
	 * @param [type] $content       [description]
	 * @param [type] $test_point_id [description]
	 */
	function add_with_test_point($content , $test_point_id)
	{
		if($test_point_id){
			$data = array(
						'content' => $content,
			 			'test_point_id' => $test_point_id);
			
		 }
		 else
		 {
		 	$data = array(
						'content' => $content,
			 			'test_point_id' => NULL);
		 }
		 $this->db->insert('knowledge_point',$data);
	}

	/**
	 * 获得某一考点下所有的条目
	 * @return [type] [description]
	 */
	function get_all_by_question_id($question_id)
	{
		$sql = "SELECT k.id as id,k.content as content,k.explanation as explanation,k.picture as picture FROM question as q,question_in_knowledge_point as qk,knowledge_point as k WHERE q.id = ? AND q.id = qk.question_id AND qk.knowledge_point_id = k.id?"; 
		$res=$this->db->query($sql,$question_id)->result_array();
		return $res;
	}

	/**
	 * 获得某一考点下所有的题目的知识点
	 * @return [type] [description]
	 */
	function get_knowledge_point_by_question_of_test_point($test_point_id)
	{
		$sql = "SELECT distinct k.id as knowledge_point_id,k.content as knowledge_point_content,k.explanation as knowledge_point_explanation,k.picture as knowledge_point_picture,t.id as test_point_id,t.content as test_point_content FROM knowledge_point as k,question_in_knowledge_point as qk,question as q,test_point as t WHERE t.id=? AND q.test_point_id=t.id AND qk.question_id=q.id AND k.id=qk.knowledge_point_id"; 
		$res=$this->db->query($sql,$test_point_id)->result_array();
		return $res;
	}
	// /**
	//  * 删除一个知识点
	//  * @param  [type] $id [description]
	//  * @return [type]     [description]
	//  */
	// function delete($id)
	// {
	// 	if($id)
	// 		$this->db->delete('knowledge_point', array('id' => $id ));

	// }

	// /**
	//  * 通过id得到一个知识点
	//  * @param  [type] $id [description]
	//  * @return [type]     [description]
	//  */
	// function get_by_id($id)
	// {
	// 	return	$res=$this->db->get_where('knowledge_point',array('id' => $id))->row_array();
	// }

	// /**
	//  * 更新一个知识点
	//  * @param  [type] $knowledge_point [description]
	//  * @return [type]                  [description]
	//  */
	// function edit($id,$knowledge_point)
	// {
		
	// 	$this->db->where('id', $id);
	// 	return	$this->db->update('knowledge_point',$knowledge_point);
		
	// }

}
